// user模块的小仓库
import {reqCategoryList} from '@/api'
import axios from 'axios'

const state ={
    categoryList:[],
    userlist:[],
    usersubgoods:[],
    // code:''
}
const mutations={
    GATEGORYLIST(state,value){
        state.categoryList = value.slice(0,16)
    },
    GATUSERINFO(state,value){
        state.userlist = value 
    },
    GATUSERSUBGOODS(state,value){
      for(var i= 0;i<value.length;i++){
        var tem = Number(String(value[i].subTime).slice(11,13))+8
        var d = tem%24 >= 10? tem%24 :'0'+tem%24
        value[i].subTime = String(value[i].subTime).slice(0,10)+ ' ' +d + String(value[i].subTime).slice(13,19)
        state.usersubgoods.push(value [i])
      }
    },
    CLEARDATA(){
      state.userlist=[]
      localStorage.removeItem('Token')
    }
    // GETCODE(state,value){
    //   state.code = value;
    // },
}
const actions={
    // 获取验证码
    // async getCode({commit},phone){
    //   // 获取验证码的这个接口是将验证码返回，但是正常情况下，后台是把验证码发到用户手机上【为了省钱】
    //   let result = await reqGetCode(phone)
    //   console.log(result)
    //   if(result.code===200){
    //       commit("GETCODE",result.data)
    //       return 'ok'
    //   }else{
    //       return Promise.reject(new Error('faile'))
    //   }
    // },
    async categoryList({commit}){
        let result = await reqCategoryList();
        // console.log(result)
        if(result.code === 200){
            commit("GATEGORYLIST",result.data)
        }
    },
    getuserlist({commit}){
        axios.get("http://localhost:8080/api/userlist/getlist",{
          params:{
            userId:localStorage.getItem('Token')
          }
        }).then(
          res=>{
            let result = res.data
            console.log("aa",result)
            if(res.status == 200){
                commit("GATUSERINFO",result[0])
            }
            console.log("a",res)

          },
          error=>{
            console.log("b",error.message)
          }
        )
    },
    getusersubGoods({commit}){
        axios.get("http://localhost:8080/api/getGoodssubmit/getuserSubgoods",{
          params:{
            id:1
          }
        }).then(
          res=>{
            let result = res.data
            console.log("xx",result)
            if(res.status == 200){
                commit("GATUSERSUBGOODS",result)
            }
            console.log("a",res)

          },
          error=>{
            console.log("b",error.message)
          }
        )
    },
    userExit({commit}){
      commit("CLEARDATA")
    }
}
const getters={
    // getCategoryList(){
    //     return state.categoryList||[]
    // }
}

export default{
    state,
    mutations,
    actions,
    getters
}